package com.study.doc.search;


import java.io.*;
import java.util.ArrayList;
import java.util.List;


public class Parser {
    private final String FILE_PATH = "D:/JAVA/jdk-8u311-docs-all/docs/api/";

    private final String ROOT_URL = "https://docs.oracle.com/javase/8/docs/api/";


    public Index index = new Index();

    public void run(){
        List<File> fileList = new ArrayList<>();
        enumFile(FILE_PATH,fileList);
        //解析所有的HTML文件,共10460个HTML文件
        for (File htmlFile : fileList) {
            parserHTML(htmlFile);
        }
        //把内存中的索引结构保存到磁盘中
        index.save();
    }

    //解析HTML文件
    public void parserHTML(File file){
        //解析标题
        String title = parserTitle(file);
        //解析URL
        String url = parserURL(file);
        //解析内容
        String content = parserContent(file);
        //System.out.println(title+"\n"+url+"\n"+content);
        index.addDoc(title,url,content);

    }
    private String parserTitle(File file){
        String fileName = file.getName();
        return fileName.substring(0,fileName.length()-".html".length());
    }

    private String parserURL(File file){
        String endPath = file.getAbsolutePath().substring(FILE_PATH.length());
        return ROOT_URL+endPath;
    }

    private String parserContent(File file){
        //读取HTML文件中的内容并转化为String类型
        String content = readFile(file);
        //替换script标签
        content = content.replaceAll("<script.*?>(.*?)</script>"," ");
        //替换HTML标签
        content = content.replaceAll("<.*?>"," ");
        //把多个空格合并成一个空格
        content = content.replaceAll("\\s+"," ");
        return  content;
    }

    private String readFile(File file){
        try (BufferedReader bufferedReader = new BufferedReader(new FileReader(file));){
            StringBuilder content = new StringBuilder();
            while (true){
                int res = bufferedReader.read();
                if (res == -1){
                    break;
                }
                char ch = (char)res;
                if (ch == '\n' || ch == '\r'){
                    ch = ' ';
                }
                content.append(ch);
            }
            return content.toString();
        } catch (IOException e) {
            e.printStackTrace();
        }
       return "";
    }


    //读取所有的HTML文件
    private void enumFile(String path, List<File> fileList){
        File rootPath = new File(path);
        File[] file =  rootPath.listFiles();
        for (File f : file) {
            if (f.isDirectory()){
                enumFile(f.getAbsolutePath(),fileList);
            }else{
                if (f.getAbsolutePath().endsWith(".html")){
                    fileList.add(f);
                }
            }
        }
    }

    public static void main(String[] args) {
        Parser parser = new Parser();
        parser.run();

    }
}
